package torres;

import torres.controller.Controller;
import torres.map.World;
import torres.view.MainView;

public class GameLoop implements Runnable {
	
	private World world;
	private MainView view;
	
	private boolean pause, inventory, abilityMenu, actionMenu, skillMenu, shopMenu, purchaseMenu, actionInventoryMenu;
	
	/**
	 * 
	 */
	public GameLoop() {
		
		view = MainView.getInstance();
		world = World.getInstance();
		world.getPlayer();
		pause = false;
		inventory = false;
		actionMenu = false;
		skillMenu = false;
		shopMenu = false;
		purchaseMenu = false;
	}
	
	/*
	public GameLoop(Collection<AI> aiCollection) {
		this();
		this.aiCollection = aiCollection;
	}
	*/
	
	/* (non-Javadoc)
	 * @see java.lang.Runnable#run()
	 */
	public void run() {
		
		while(World.getInstance().getPlayer().getLives() >= 0) {
			if (!pause) {
				loop();
			}
		}
		
		view.dispose();
	}
	
	/**
	 * 
	 */
	public void loop() {
		
		World.getInstance().getCurrentMap().runAI();
		
		World.getInstance().getPlayer().checkLocation(World.getInstance().getPlayer().getLocation());
		
		Controller.process();
		
		Long start = System.nanoTime();
		
		view.draw();
		
		Long difference = System.nanoTime() - start;
		difference /= 1000000;
		difference = 68 - difference;
		try {
			
			if(difference>0) {
				
				Thread.sleep(difference);
			}
		}
		catch(InterruptedException exception) { 
			
			exception.printStackTrace(); 
		}
	}
	
	/**
	 * 
	 */
	public void togglePause() {
		pause = !pause;
		//System.out.println("togglePause: " + pause);
	}
	
	
	/**
	 * @return
	 */
	public boolean getPause() {
		return pause;
	}

	/**
	 * 
	 */
	public void toggleInventory() {
		inventory = !inventory;
		//System.out.println("toggleInventory: " + inventory);
	}
	
	/**
	 * @return
	 */
	public void toggleActionMenu() {
		actionMenu = !actionMenu;
	}
	
	public boolean getInventory() {
		return inventory;
	}
	
	/**
	 * 
	 */
	public void toggleAbilityMenu() {
		abilityMenu = !abilityMenu;
		//System.out.println("toggleInventory: " + inventory);
	}

	/**
	 * @param abilityMenu the abilitymenu to set
	 */
	public void setAbilityMenu(boolean abilityMenu) {
		this.abilityMenu = abilityMenu;
	}

	/**
	 * @return the abilityMenu
	 */
	public boolean getAbilityMenu() {
		return abilityMenu;
	}
	
	public boolean getActionMenu() {
		return actionMenu;
	}

	public boolean getSkillMenu() {
		return skillMenu;
	}

	public void toggleSkillMenu() {
		skillMenu = !skillMenu;
	}

	public boolean getShopMenu() {
		return shopMenu;
	}

	public void toggleShopMenu() {
		shopMenu = !shopMenu;
	}
	
	public boolean getPurchaseMenu() {
		return purchaseMenu;
	}

	public void togglePurchaseMenu() {
		purchaseMenu = !purchaseMenu;
	}
	
	public void toggleActionInventoryMenu() {
		actionInventoryMenu = !actionInventoryMenu;
	}

	public boolean getActionInventoryMenu() {
		return actionInventoryMenu;
	}
}
